<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/5
 * Time: 16:25
 */
class Repair extends Model
{

    /**
     * 报修显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function repairList(){
        $data = input();
        $where=[];
        $page=isset($data['page']) ? trim($data['page']) : 1;
        $number=isset($data['number']) ? trim($data['number']) : 10;
        $status=isset($data['status']) ? trim($data['status']) : '';
        $tung_num=isset($data['tung_num']) ? trim($data['tung_num']) : '';
        $unit_num=isset($data['unit_num']) ? trim($data['unit_num']) : '';
        $room_num=isset($data['room_num']) ? trim($data['room_num']) : '';
        $time=isset($data['time']) ? trim($data['time']) : '';
        $where['admin_id'] = create_id($data['token']);
        if($status!=null){
            $where['status'] = array('eq',$status);
        }
        if($tung_num!=null){
            $where['tung_num'] = array('eq',$tung_num);
        }
        if($unit_num!=null){
            $where['unit_num'] = array('eq',$unit_num);
        }
        if($room_num!=null){
            $where['room_num'] = array('eq',$room_num);
        }
        if ($time !== "") {
            $op = 'between';
            switch ($time) {
                case 1:
                    $range = ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'];
                    break;
                case 2:
                    $range = ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'];
                    break;
                case 3:
                    $op = '>';
                    $range = '-3 months';
                    break;
                default:
            }
            $count=Db::name('repair')->where($where) ->whereTime('create_time', $op, $range)->count();
            $data=Db::name('repair')->where($where)
                ->whereTime('create_time', $op, $range)
                ->field('repair_id,room_number,tenant_name,tenant_tel,title,status,create_time,maintenance_id')
                ->page($page, $number)
                ->select();
        }else{
            $count=Db::name('repair')->where($where) ->count();
            $data=Db::name('repair')->where($where)
                ->field('repair_id,room_number,tenant_name,tenant_tel,title,status,create_time,maintenance_id')
                ->page($page, $number)
                ->select();
        }
        foreach ($data as $k=>$v) {
            $admin_user=Db::name('admin_user')->where(['admin_id'=>$v['maintenance_id']])
                ->field('name')
                ->find();
            $data[$k]['name']=$admin_user['name'];
        }
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }

    /**
     * 报修编辑
     * @param $id 报修id
     * @return array|false|\PDOStatement|string|Model
     */
     public function repairOne($id){
         $repair=Db::name('maintenance_records')->where(['m_id'=>$id])->find();
         if($repair==null){
             return 2;
         }
         if($repair['m_image']!=null){
             $repair['m_image']=explode(',',$repair['m_image']);
         }
         return $repair;
     }
    /**
     * 维修记录编辑提交
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function recordEditor($data){
        $where['m_id']=$data['m_id'];
        $arr['m_remark'] = isset($data['remark']) ? trim($data['remark']) : "";
        $arr['m_image'] = isset($data['image']) ? implode(',',$data['image']): "";
        $repair=Db::name('maintenance_records')->where($where)->update($arr);
        return $repair;
    }
    /**
     * 维修记录编辑提交
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function recordAdd($data){
        $arr['u_id']=get_user_id($data['token']);
        $arr['r_id'] = $data['repair_id'];
        $arr['m_remark'] = isset($data['remark']) ? trim($data['remark']) : "";
        $arr['m_image'] = isset($data['image']) ? implode(',',$data['image']): "";
        $arr['m_deal_time'] = time();
        $repair=Db::name('maintenance_records')->insert($arr);
        return $repair;
    }

    /**
     * 维修记录删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function recordDel($data){
        $where['m_id']=$data['m_id'];
        $repair=Db::name('maintenance_records')->where($where)->delete();
        return $repair;
    }
    /**
     * 报修编辑提交
     * @param $data
     * @return array|false|\PDOStatement|string|Model
     */
    public function repairConfirm($data){
        $uid=get_user_id($data['token']);
        $where['repair_id'] =$data['repair_id'] ;
        $count['remark'] =$data['remark'] ;
        $count['status'] =$data['status'] ;
        $count['maintenance_id'] =$uid ;
        $count['deal_time'] =time() ;
        $data=Db::name('repair')->where($where)->update($count);
        return $data;
    }
    /**
     * 报修查看
     * @param $datas 报修id
     * @return array|false|\PDOStatement|string|Model
     */
    public function PayRentWithdrawal($datas){
        $id = trim( $datas['repair_id'] );//报修id
        $wheres['admin_id'] = create_id($datas['token']);
        $data=Db::name('repair')
            ->field('repair_id,room_number,tenant_name,tenant_tel,title,status,create_time,image,maintenance_id,remark')
            ->where(['repair_id'=>$id])
            ->where($wheres)
            ->find();
        if($data['image']!=null){
            $data['image']=explode(',',$data['image']);
        }
        //权限表
        $action=Db::name('action')->where(['id'=>121])->find();
        //模糊查询权限角色表
        $where['action_id'] = ['like',"%".$action['id']."%"];
        $role=Db::name('role_action')->field('role_id')->where($where)->select();
       // var_dump($role);exit;
        $count=[];
        //循环拿到权限对应员工
        foreach ($role as $k=>$v){
            $date=Db::name('admin_user')
                ->field('admin_id,job_number,admin_tel,name')
                ->where(['create_id'=>$wheres['admin_id']])
                ->where(['role_id'=>$v['role_id']])
                ->select();
            if($date!=null){
                $count[]=$date;

            }
        }
        $newhello=[];
        $k=0;
        foreach ($count as $key => $val) {
            foreach ($val as $key2 => $val2) {
                $newhello[$k]['admin_id'] = $val2 ['admin_id'];
                $newhello[$k]['job_number'] = $val2['job_number'];
                $newhello[$k]['admin_tel'] = $val2['admin_tel'];
                $newhello[$k]['name'] = $val2['name'];
                $k++;
            }
        }
        $data['admin_user']=$newhello;
      //  var_dump($newhello);exit;
        $admin_user=Db::name('admin_user')->where(['admin_id'=>$data['maintenance_id']])
            ->field('admin_id,name,job_number')
            ->find();
        $data['admin_id']=$admin_user['admin_id'];
        $data['name']=$admin_user['name'];
        $data['number']=$admin_user['job_number'];
        $repair=Db::name('maintenance_records')
            ->where(['r_id'=>$datas['repair_id']])
            ->select();
        foreach($repair as $k=>$v){
           $name=Db::name('admin_user')->where(['admin_id'=>$v['u_id']])
                ->field('name')
                ->find();
            $repair[$k]['name']=$name['name'];
            if($v['m_image']!=null){
                $repair[$k]['m_image']=explode(',',$v['m_image']);
            }
        }
        $data['records']=$repair;
        return $data;
    }
    /**
     * 指派
     * @param $data
     * @return array|false|\PDOStatement|string|Model
     */
    public function assign($data){
        $where['repair_id'] =$data['repair_id'] ;
        $count['maintenance_id'] =$data['user_id'] ;
        $count['status'] =3;
        $i=Db::name('repair')->where($where)->find();
       if($i['maintenance_id']!=null){
           $data=Db::name('repair')->where($where)->update($count);
       }else{
           $count['deal_time'] =time() ;
           $data=Db::name('repair')->where($where)->update($count);
           $wei=[];
           $admin_user=Db::name('admin_user')->where(['admin_id'=>$i['maintenance_id']])->find();
           $user=Db::name('house_keeper')->where(['id'=>$i['user_id']])->find();
           $wei['openid']=$user['wechat_subscription_open_id'];
           if($user['sex']==1){
               $wei['first']="尊敬的".$i['tenant_name']."先生，您的报修有新进展";
           }else{
               $wei['first']="尊敬的".$i['tenant_name']."女士，您的报修有新进展";
           }
           $wei['keyword1']=$i['room_number'];
           $wei['keyword2']=$i['title'];
           $wei['keyword4']='处理中';
           $wei['keyword5']='已安排维修工'.$admin_user['name'].'上门维修，联系电话:'.$admin_user['admin_tel'];
           $wei['keyword3']=date('Y-m-d H:i:s',$i['create_time']);
           $data['url']='pages/mine/repair/repairDetail/repairDetail?id='.$data['repair_id'].'&type=2';
           $model=new \app\applet\model\Repair();
           $model->Wechat($wei);
       }
        return 1;
    }
    /**
     * 维修完成
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function maintenanceCompleted($data){
        $where['repair_id']=$data['repair_id'];
        $arr['status']=1;
        $arr['over_time']=time();
        $repair=Db::name('repair')->where($where)->update($arr);
        $i=Db::name('repair')->where($where)->find();
        $user=Db::name('admin_user')->where(['admin_id'=>$i['maintenance_id']])->find();
        $keeper=Db::name('house_keeper')->where(['id'=>$i['user_id']])->find();
        $coun['first']='尊敬的租客，您的报修已经完成';
        $coun['remark']='点击查看报修情况。';
        $coun['keyword1']=$i['room_number'];
        $coun['keyword2']=$i['title'];
        $coun['keyword3']=$user['name'];
        $coun['openid']=$keeper['wechat_subscription_open_id'];
        $coun['keyword4']=date("Y-m-d",$i['over_time']);
        $coun['url']='pages/mine/repair/repairDetail/repairDetail?id='.$data['repair_id'].'&type=3';
        $model=new \app\applet\model\Repair();
        $model->WechatTwo($coun);
        return $repair;
    }

}